import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Home from "../views/home/Home";
import Category from "../views/category/Category";
import ShopCart from "../views/shopcart/ShopCart";
import Profile from "../views/profile/Profile";
import GoodsDetail from "../views/detail/GoodsDetail";
import Register from "../views/profile/Register";
import Login from "../views/profile/Login";
import SetUserInfo from "../views/profile/SetUserInfo";
import addAddress from "../views/profile/addAddress";
import addressList from '../views/profile/AddressList';
import EditAddress from "../views/profile/EditAddress";
import OrderPreview from "../views/order/OrderPreview";
import OrderDetail from "../views/order/OrderDetail";
import OrderList from "../views/order/OrderList";
import MyCollection from '../views/profile/myCollection'
import {Toast} from "vant";

const routes = [
    {
        path:'/',
        redirect:'/home'
    },
    {
        path:'/home',
        name:'Home',
        component:Home,
        meta:{
            title:'图书兄弟-首页'
        }
    },
    {
        path:'/category',
        name:'Category',
        component: Category,
        meta:{
            title:'图书兄弟-分类'
        }
    },
    {
        path:'/shopCart',
        name:'ShopCart',
        component: ShopCart,
        meta:{
            title:'图书兄弟-购物车',
            isAuthorization:true
        }
    },
    {
        path:'/profile',
        name:'Profile',
        component: Profile,
        meta:{
            title: '图书兄弟-个人中心'
        }
    },
    {
        path:'/goodsDetail',
        name:'GoodsDetail',
        component:GoodsDetail,
        meta:{
            title: '图书兄弟-图书详情'
        }
    },
    {
        path:'/register',
        name:'Register',
        component: Register,
        meta:{
            title: '图书兄弟-用户注册'
        }
    },
    {
        path: '/Login',
        name:'Login',
        component: Login,
        meta:{
            title: '图书兄弟-用户登录'
        }
    },
    {
        path:"/setUserInfo",
        name:'setUserInfo',
        component: SetUserInfo,
        meta:{
            title: '图书兄弟-修改用户信息'
        }
    },
    {
        path:'/addAddress',
        name:'addAddress',
        component: addAddress,
            meta:{
                title:'图书兄弟-添加用户地址'
            }
    },
    {
        path:'/addressList',
        name:'addressList',
        component:addressList,
            meta:{
                title:'图书兄弟-地址列表'
            }

    },
    {
        path:'/editAddress',
        name:'editAddress',
        component: EditAddress,
        meta:{
            title: '图书兄弟-修改用户地址'
        }
    },
    {
        path:'/orderPreview',
        name: 'orderPreview',
        component: OrderPreview,
        meta: {
            title: '图书兄弟-订单预览'
        }
    },
    {
        path:'/orderDetail',
        name: 'orderDetail',
        component: OrderDetail,
        meta:{
            title: '图书兄弟-订单详情'
        }
    },
    {
        path:'/orderList',
        name: 'orderList',
        component: OrderList,
        meta:{
            title: '图书兄弟-订单列表'
        }
    },
    {
        path:'/myCollection',
        name: 'myCollection',
        component: MyCollection,
        meta:{
            title: '图书兄弟-收藏列表'
        }
    }




]

const router = new VueRouter({
    routes,
    mode:'hash',
    scrollBehavior(to, from, savedPosition) {
        if(savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
})

router.beforeEach((to,from,next)=>{
    const token = window.localStorage.getItem('token')
    document.title = to.meta.title
    if(to.meta.isAuthorization && !token){
        Toast('请先登录')
        next({path:'/login'})
    }else {
        // console.log(to);
        next()
        document.title = to.meta.title
    }

})



export default router
